if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready',plusReady,false);
}

//号位分工的乘务员姓名下拉框的onchange响应函数
function setNameToLabel(id,value){
		$(id).text(value);
}

function plusReady () {
	
	//初始化选择日期
	$(".date_init").val(function() {		
		return date_formate(new Date());
	});
	
	var date = $(".date_init").val();
	var userInfo = getData("user_data");
	var item = getData("item");
	
	var MPS_list = [];
	var SC_list = [];
	var ST_list = [];
	var FP_list = [];
	var A1_list = [];
	var crewlinkline = "";
	
	$(document).ready(function() {
		UpdateState();
		$(".form_datetime").on("click", function () {
			var form_datetime = $(this);
			var d = new Date();
			var dates = form_datetime.val().split("-")
			d.setFullYear(parseInt(dates[0]), parseInt(dates[1])-1, parseInt(dates[2]))
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				form_datetime.val(date_formate(d));
				date = form_datetime.val();
				table_empty();
				table_init ();
				flight_init ();
			}, function(e){
				plus.nativeUI.alert( "未选择日期!");
			}, {title:"请选择日期", date:d});
		});
		if (!isOnLine) {
			plus.nativeUI.alert("请在联网的情况下填写乘务日志！");
			window.location.href = "flight_log_list.html";
			return;
		}
		if (!item) {
			flight_init ();
		} else{
			load_data ();
		}
		table_init ();
		
		$("#flightvoyage").on("change",function () {
			
			loadInfo ();
		});
		
		$("#fligtOntimeDetail").on("click", "input[name='timePicker']", function () {
			var timePicker = $(this);
			var d = new Date();
			var times = timePicker.val().split(":")
			d.setHours(parseInt(times[0]), parseInt(times[1]))
			plus.nativeUI.pickTime( function(e){
				timePicker.val(time_formate(e.date));
			}, function(e){
				plus.nativeUI.alert( "未选择日期!");
			}, {title:"请选择时间", time:d});
			
		});
		
		$("#flightLateDetail").on("click", "input[name='timePicker']", function () {
			var timePicker = $(this);
			var d = new Date();
			var times = timePicker.val().split(":")
			d.setHours(parseInt(times[0]), parseInt(times[1]))
			plus.nativeUI.pickTime( function(e){
				timePicker.val(time_formate(e.date));
			}, function(e){
				plus.nativeUI.alert( "未选择日期!");
			}, {title:"请选择时间", time:d});
			
		});
		
		//取消编辑
		$("#cancle_log").on("click", function () {
			plus.nativeUI.confirm("您确定放弃本次填写吗？",function (event) {
				if (event.index == 0) {
					removeItem("item");
					window.location.href = "flight_log_list.html";
				}
			});
		});
		
		//确认保存
		$("#confirm_log").on("click", function () {
			if (getData(item).uploaded) {
				plus.nativeUI.alert("无法修改已上传的日志！");
				window.location.href = "flight_log_list.html";
			} else if (save_data ()) {
				plus.nativeUI.alert("保存成功！");
				window.location.href = "flight_log_list.html";
			}
		});	
	});
		
	function setDataById (id, content) {
		id = "#" + id;
		$(id).val(content);
	}
	function getDataById (id) {
		id = "#" + id;
		return $(id).val()==undefined?null:$(id).val();
	}
	function return_p_code (list, c_name) {
		for (var i=0; i<list.length; i++) {
			if(list[i].c_name == c_name) {
				return list[i].p_code;
			}
		}
		return null;
	}
	function return_c_name (list, p_code) {
		for (var i=0; i<list.length; i++) {
			if(list[i].p_code == p_code) {
				return list[i].c_name;
			}
		}
		return null;
	}
	
	//根据日期与用户id获得航班信息
	function flight_init () {
		date = $(".date_init").val();
		var url = server_url+'/9airweb/getairline?usercode='+userInfo.msg.id+'&flightdate='+date;
		url += url_tail;
		console.log(url);
		
		$.ajax({
				dataType: "json",
			    url: url,
			    timeout:15000
		}).done(function(backdata, status){
			if (status=='success') {
				if (setData('LogDetail'+date+userInfo.msg.id, backdata)) {
					console.log("数据更新成功！");
					loadInfo();
				}
			}
		}).fail(function(jqXHR, textStatus){
		    if(textStatus == 'timeout') {
		        plus.nativeUI.alert("服务器异常，请稍后重试！");
		        window.location.href = "flight_log_list.html";
		    }
		});
	}
	
	function loadInfo () {
		var info = getData('LogDetail'+date+userInfo.msg.id);
		
		function loadinfoall (num) {
			crewlinkline = info.msg[num].crewlinkline;
			people_init (crewlinkline);
			setDataById ("actype", info.msg[num].actype);
			setDataById ("flightno", info.msg[num].acreg);
			setDataById ("flightcom", "AQ"+info.msg[num].flightcom);
			var option = "";
			for (var i=0; i<info.msg.length; i++) {
				if (i == num) {
					option += '<option selected="selected">'+info.msg[i].flightvoyage+'</option>';
				} else{
					option += '<option>'+info.msg[i].flightvoyage+'</option>';
				}
			}
			$("#flightvoyage").empty();
			$("#flightvoyage").append(option);
			
		}
		if (info.type != "success") {
			plus.nativeUI.alert(info.msg);
		} else {
			
			for (var i=0; i<info.msg.length; i++) {
				if ($("#flightvoyage").val()==null) {
					loadinfoall(0);
				} else if ($("#flightvoyage").val() == info.msg[i].flightvoyage) {
					loadinfoall(i);
				
				}
			}
			
		}
	}
	
	//根据日期与航线获得机组人员
	function people_init (crewlinkline) {
		date = $(".date_init").val();
		var url = server_url+'/9airweb/getgroup?crew_link_line='+crewlinkline+'&flightdate='+date;
		url += url_tail;
		console.log(url);
		
		$.ajax({
				dataType: "json",
			    url: url,
			    timeout:15000
		}).done(function(backdata, status){
			if (status=='success') {
				if (setData('LogDetail'+date+crewlinkline+userInfo.msg.id, backdata)) {
					console.log("数据更新成功！");
					loadInfo();
				}
			}
		}).fail(function(jqXHR, textStatus){
		    if(textStatus == 'timeout') {
		        plus.nativeUI.alert("数据加载失败！");
		    }
		});
		
		function loadInfo () {
			var info = getData('LogDetail'+date+crewlinkline+userInfo.msg.id);
			if (info.type != "success") {
				plus.nativeUI.alert(info.msg);
			} else{
				info = info.msg;
				SC_list.length = 0;
				ST_list.length = 0;
				FP_list.length = 0;
				MPS_list.length = 0;
				A1_list.length = 0;
				$.each(info,function (key, value) {
					if (value.rank_no == "SC") {
						SC_list[SC_list.length] = {"c_name":value.c_name,"p_code":value.p_code}; 
					} else if (value.rank_no == "ST") {
						ST_list[ST_list.length] = {"c_name":value.c_name,"p_code":value.p_code}; 
					} else if (value.rank_no == "FP") {
						FP_list[FP_list.length] = {"c_name":value.c_name,"p_code":value.p_code}; 
					} else if (value.rank_no == "MPS") {
						if (userInfo.msg.id == value.p_code) {
							MPS_list[MPS_list.length] = {"c_name":value.c_name,"p_code":value.p_code}; 
						} else{
							plus.nativeUI.alert("本次航班您不是乘务长，无法填写乘务日志！");
							window.location.href = "index.html";
						}
						
					} else if (value.rank_no == "A001") {
						A1_list[A1_list.length] = {"c_name":value.c_name,"p_code":value.p_code};
					}
				});
				
				//加载机长
				setDataById ("pilot", A1_list[0].c_name);
				
				//加载乘务长
				var option="";
				for (var j=0; j<MPS_list.length; j++ ) {
					option += '<option>'+MPS_list[j].c_name+'</option>'
				}
				$("#mps").empty();
				$("#mps").append(option);
				
				//加载二号至六号乘务员
				for (var i=2; i<=6; i++) {
					var option="";
					for (var j=0; j<SC_list.length; j++ ) {
						if (i == j+2) {
							option += '<option selected="selected">'+SC_list[j].c_name+'</option>'
						} else{
							option += '<option>'+SC_list[j].c_name+'</option>'
						}
					}
					if (SC_list.length+2 <= i) {
						option = '<option>无</option>';
					}
					$(".sc"+i).empty();
					$(".sc"+i).append(option);
					
				}
				//设置乘务员航班情况考评的姓名标签
				for (var i=2; i<=6; i++) {
					if(i - 2 >= SC_list.length){
						$("#label_sc"+i).text("无");
					}else{
						$("#label_sc"+i).text(SC_list[i-2].c_name);
					}
				}
				//设置岗位明星下拉框
				option = "";
				for(var j=0;j<SC_list.length;j++){
					option += '<option>'+SC_list[j].c_name+'</option>'
				}
				if(option != ""){
					option += '<option selected="selected"></option>'
					$("#id_safe_star").append(option);
					$("#id_srvs_star").append(option);
					$("#id_img_star").append(option);
					$("#id_sml_star").append(option);
					$("#id_ep_star").append(option);
					$("#id_sale_star").append(option);			
				}
		
				//加载乘务学员
				for (var i=1; i<=2; i++) {
					var option="";
					for (var j=0; j<ST_list.length; j++) {
						if (i == j+1) {
							option += '<option selected="selected">'+ST_list[j].c_name+'</option>'
						} else{
							option += '<option>'+ST_list[j].c_name+'</option>'
						}
					}
					if (ST_list.length < i) {
						option = '<option>无</option>';
					}
					$("#st"+i).empty();
					$("#st"+i).append(option);
				}
				
				//加载航班正常 详细描述
				var table = '';
				var flightNos = getDataById("flightcom").replace("AQ", "").split("/");
				var flightInfo = getData("FlightInfo"+date+userInfo.msg.username).msg;
				if(flightInfo != undefined){
					        table = '<tr><td>航班号</td><td>上客</td><td>上完客</td><td>关舱</td><td>开舱</td><td>下客</td><td>下完客</td></tr>';
					for (var i=0; i<flightNos.length; i++) {
					        for (var j=flightInfo.length-1; j>=0; j--) {
						        if ("AQ"+flightNos[i] == flightInfo[j].flightNo) {
							table += '<tr>';
							table += '<td><input type="text" class="form-control" value="'+ flightInfo[j].flightNo +'" /></td>'
							table += '<td><input type="text" class="form-control" name="timePicker" value="'+ flightInfo[j].origtime +'" style="background-color: white;"  readonly /></td>'
							table += '<td><input type="text" class="form-control" name="timePicker" value="'+ flightInfo[j].origtime +'" style="background-color: white;"  readonly /></td>'
							table += '<td><input type="text" class="form-control" name="timePicker" value="'+ flightInfo[j].origtime +'" style="background-color: white;" readonly /></td>'
							table += '<td><input type="text" class="form-control" name="timePicker" value="'+ flightInfo[j].destime +'" style="background-color: white;" readonly /></td>'
							table += '<td><input type="text" class="form-control" name="timePicker" value="'+ flightInfo[j].destime +'" style="background-color: white;" readonly /></td>'
							table += '<td><input type="text" class="form-control" name="timePicker" value="'+ flightInfo[j].destime +'" style="background-color: white;" readonly /></td>'
							table += '</tr>';
						    }
					    }
				    }
				}
				table += '<tr><td colspan="7"><textarea class="form-control" placeholder="其他说明" cols=""></textarea></td></tr>';
				$("#fligtOntimeDetail").empty();
				$("#fligtOntimeDetail").append(table);
				
				$("#flightLateDetail").empty();
				$("#flightLateDetail").append(table);
				
				//加载安全员
				var safe = "";
				for (var i = 0; i<FP_list.length; i++) {
					safe += FP_list[i].c_name + " / ";
				}

				safe = safe.substring(0,safe.length-3);
				setDataById("safe", safe);
				
				//加载安全员打分机制
				for(var i = 0;i <　FP_list.length; i++){
					var index = i+1;
					$("#show_fp"+index).show();
					$("#fp"+index).text(FP_list[i].c_name);					
				}
			}
		}
	}
	
	function save_data () {
		var log = {};
		if ($("#jijian").attr("checked") == "checked") {
			log.jijian = "1";
		} else{
			log.jijian = "0";
		}
		log.flight_date = getDataById("log_date");
		log.ac_type = getDataById("actype");
		log.flight_no = getDataById("flightno");
		log.pilot = return_p_code(A1_list, getDataById("pilot"));
		//不存入数据库
		log.safe = getDataById("safe");
		log.flight_com = getDataById("flightcom").replace("AQ","");
		log.flight_voyage = getDataById("flightvoyage");
		
		log.userCode = return_p_code(MPS_list, getDataById("mps"));
		log.purser2 = return_p_code(SC_list, getDataById("sc2"));
		log.purser3 = return_p_code(SC_list, getDataById("sc3"));
		log.purser4 = return_p_code(SC_list, getDataById("sc4"));
		log.purser5 = return_p_code(SC_list, getDataById("sc5"));
		log.purser6 = return_p_code(SC_list, getDataById("sc6"));
		
		log.purser_trainee1 = return_p_code(ST_list, getDataById("st1"));
		log.purser_trainee2 = return_p_code(ST_list, getDataById("st2"));
		
		log.passengers_num1 = getDataById("passengers_num1")==""?0:parseInt(getDataById("passengers_num1"));
		log.passengers_num2 = getDataById("passengers_num2")==""?0:parseInt(getDataById("passengers_num2"));
		log.passengers_num3 = getDataById("passengers_num3")==""?0:parseInt(getDataById("passengers_num3"));
		log.passengers_num4 = getDataById("passengers_num4")==""?0:parseInt(getDataById("passengers_num4"));
		log.passengers_num5 = getDataById("passengers_num5")==""?0:parseInt(getDataById("passengers_num5"));
		log.passengers_num6 = getDataById("passengers_num6")==""?0:parseInt(getDataById("passengers_num6"));
		
		if ($("#ontime").attr("checked") == "checked") {
			log.flight_instance = "1";
			var len = $("#fligtOntimeDetail input").length;
			var html = '';
			if(len != 0){
				html += "航班号 上客 上完客 关舱 开舱 下客 下完客\n";
				$("#fligtOntimeDetail input").each(function (index) {
				if(index == len -1){
					html += $(this).val();
				}else{
					html += $(this).val()+',';
				}
			});
			html += ";"+$("#fligtOntimeDetail textarea").val();
			}else{
				html = $("#fligtOntimeDetail textarea").val();
			}
			log.flight_instance_log = html;
		} else if($("#late").attr("checked") == "checked") {
			log.flight_instance = "2";
			
			log.flight_instance_n_type = "";
			$("input[name='flightLateType']:checked").each(function () {
				var LateType = "" + $(this).attr("id");
				log.flight_instance_n_type += LateType.replace("LateType", "");
			});
			
			var LateTime = $("input[name='flightLateTime']:checked").attr("id");
			log.flight_instance_n_time = (LateTime==undefined?null:LateTime.replace("LateTime", ""));
			var len = $("#flightLateDetail input").length;
			var html = '';
			if(len != 0){
				html += '航班号 上客 上完客 关舱 开舱 下客 下完客\n';
				$("#flightLateDetail input").each(function (index) {
				if(index == len - 1){
					html += $(this).val();
				}else{
					html += $(this).val()+',';
				}
			});
			   html += ";"+$("#flightLateDetail textarea").val();
			}else{
				html += $("#flightLateDetail textarea").val();
			}
			log.flight_instance_log_n = html;
		} else{
			plus.nativeUI.alert("请选择航班情况！");
			return false;
		}
		
		log.exception_old = getDataById("exception_old")==""?0:parseInt(getDataById("exception_old"));
		log.exception_child = getDataById("exception_child")==""?0:parseInt(getDataById("exception_child"));
		log.exception_longya = getDataById("exception_longya")==""?0:parseInt(getDataById("exception_longya"));
		log.exception_chair = getDataById("exception_chair")==""?0:parseInt(getDataById("exception_chair"));
		log.exception_vip = getDataById("exception_vip")==""?0:parseInt(getDataById("exception_vip"));
		log.exception_other = getDataById("exception_other")==""?0:parseInt(getDataById("exception_other"));
		
		log.exceptions = getDataById("exceptions");
		
		log.safe_event = "";
		$("input[name='safeEventType']:checked").each(function () {
			var safeEvent = $(this).attr("id");
			log.safe_event += (safeEvent==undefined?"":safeEvent.replace("safeEvent", ""));
		});
		$("input[name='aidEventType']:checked").each(function () {
			var safeEvent = "" + $(this).attr("id");
			log.safe_event += (safeEvent==undefined?"":safeEvent.replace("safeEvent", ""));
		});
		
		log.safe_event_result = getDataById("safe_event_result");
		
		log.dept_pc = getDataById("dept_pc");
		log.dept_sw = getDataById("dept_sw");
		log.succestions = getDataById("succestions");
		
		log.sms_report = getDataById("sms_report");
		
		log.check_mark2 = parseInt(getDataById("check_mark2"));
		log.check_mark3 = parseInt(getDataById("check_mark3"));
		log.check_mark4 = parseInt(getDataById("check_mark4"));
		log.check_mark5 = parseInt(getDataById("check_mark5"));
		log.check_mark6 = parseInt(getDataById("check_mark6"));
		
		log.check_content2 = getDataById("check_content2");
		log.check_content3 = getDataById("check_content3");
		log.check_content4 = getDataById("check_content4");
		log.check_content5 = getDataById("check_content5");
		log.check_content6 = getDataById("check_content6");
		log.security1 = return_p_code(FP_list, $("#fp1").text());
		log.check_mark_security1 = parseInt(getDataById("check_mark_security1"));
		log.check_content_security1 = getDataById("check_content_security1");
		
		if ($("#fp2").text() != "") {
			log.security2 = return_p_code(FP_list, $("#fp2").text());
			log.check_mark_security2 = parseInt(getDataById("check_mark_security2"));
			log.check_content_security2 = getDataById("check_content_security2");
		}
		
		if ($("#fp3").text() != "") {
			log.security3 = return_p_code(FP_list, $("#fp3").text());
			log.check_mark_security3 = parseInt(getDataById("check_mark_security3"));
			log.check_content_security3 = getDataById("check_content_security3");
		}
		//岗位明星评比
		log.safe_star = return_p_code(SC_list, getDataById("id_safe_star"));
		log.srvs_star = return_p_code(SC_list, getDataById("id_srvs_star"));
		log.img_star = return_p_code(SC_list, getDataById("id_img_star"));
		log.sml_star = return_p_code(SC_list, getDataById("id_sml_star"));
		log.ep_star = return_p_code(SC_list, getDataById("id_ep_star"));
		log.sale_star = return_p_code(SC_list, getDataById("id_sale_star"));
		
		//不存入数据库
		log.MPS_list = MPS_list;
		log.SC_list = SC_list;
		log.ST_list = ST_list;
		log.FP_list = FP_list;
		log.A1_list = A1_list;
		
		if (!item) {
			setData("LogList"+userInfo.msg.id+(showLogListMax(userInfo.msg.id)+1), log);
			return true;
		} else{
			removeItem(item);
			setData(item, log);
			return true;
		}
	}
	
	function load_data () {
		if (!item) {
			return;
		} else{
			var log = getData(item);
			
			MPS_list = log.MPS_list;
			SC_list = log.SC_list;
			ST_list = log.ST_list;
			FP_list = log.FP_list;
			A1_list = log.A1_list;
			
			if (log.jijian == "1") {
				$("#jijian").attr("checked", true);
			}
			$(".date_init").val(log.flight_date);
			setDataById("actype", log.ac_type);
			setDataById("flightno", log.flight_no);
			setDataById("pilot", return_c_name(A1_list,log.pilot));
			setDataById("safe", log.safe);
			setDataById("flightcom", "AQ"+log.flight_com);
			$("#flightvoyage").empty(); //add
			$("#flightvoyage").html("<option>"+log.flight_voyage+"</option>>");
			
			$("#mps").append("<option>"+return_c_name(MPS_list,log.userCode)+"</option>>");
			
			//加载二号至六号乘务员
			var purser = [];
			purser[0] = return_c_name(SC_list, log.purser2);
			purser[1] = return_c_name(SC_list, log.purser3);
			purser[2] = return_c_name(SC_list, log.purser4);
			purser[3] = return_c_name(SC_list, log.purser5);
			purser[4] = return_c_name(SC_list, log.purser6);
			for (var i=2; i<=6; i++) {
				var option="";
				for (var j=0; j<purser.length; j++ ) {
					if (purser[i-2] == null) {
						option = '<option>无</option>';
						break;
					}
					if (purser[j] == null) {
						break;
					} else if (i == j+2) {
						option += '<option selected="selected">'+purser[j]+'</option>'
					} else{
						option += '<option>'+purser[j]+'</option>'
					}
				}
				$(".sc"+i).append(option);
			}
			
			//设置乘务员航班情况考评的姓名标签
				for (var i=2; i<=6; i++) {
					if(purser[i-2] == null){
						$("#label_sc"+i).text("无");
					}else{
						$("#label_sc"+i).text(purser[i-2]);
					}
				}
				
			//设置岗位明星下拉框
				var option = "";
				for(var j=0;j<SC_list.length;j++){
					option += '<option>'+SC_list[j].c_name+'</option>'
				}
				if(option != ""){
					option += '<option selected="selected"></option>'
					$("#id_safe_star").append(option);
					$("#id_srvs_star").append(option);
					$("#id_img_star").append(option);
					$("#id_sml_star").append(option);
					$("#id_ep_star").append(option);
					$("#id_sale_star").append(option);	
					$("#id_safe_star").val(return_c_name(SC_list, log.safe_star));
				    $("#id_srvs_star").val(return_c_name(SC_list, log.srvs_star));
				    $("#id_img_star").val(return_c_name(SC_list, log.img_star));
				    $("#id_sml_star").val(return_c_name(SC_list, log.sml_star));
				    $("#id_ep_star").val(return_c_name(SC_list, log.ep_star));
				    $("#id_sale_star").val(return_c_name(SC_list, log.sale_star));
				}
				
			//加载乘务学员
			var purser_trainee = [];
			purser_trainee[0] = return_c_name(ST_list, log.purser_trainee1);
			purser_trainee[1] = return_c_name(ST_list, log.purser_trainee2);
			
			for (var i=1; i<=2; i++) {
				var option="";
				if (ST_list.length == 0) {
					option +='<option selected="selected">无</option>';
				} else if (ST_list.length == 1) {
					if (i == 1) {
						option += '<option selected="selected">'+purser_trainee[0]+'</option>'
					} else{
						option +='<option selected="selected">无</option>';
					}
				} else{
					for (var j=0; j<ST_list.length; j++ ) {
						if (i == j+1) {
							option += '<option selected="selected">'+purser_trainee[j]+'</option>'
						} else{
							option += '<option>'+purser_trainee[j]+'</option>'
						}
					}
				}
				$("#st"+i).append(option);
			}
			//加载安全员打分机制
			var security = [];
			security[0] = return_c_name(FP_list, log.security1);
			security[1] = return_c_name(FP_list, log.security2);
			security[2] = return_c_name(FP_list, log.security3);
			for (var i = 0; i< FP_list.length; i++) {
				var index = i + 1;
				$("#show_fp" + index).show();
				$("#fp" + index).text(security[i]);
			}
			
			setDataById("passengers_num1",log.passengers_num1);
			setDataById("passengers_num2",log.passengers_num2);
			setDataById("passengers_num3",log.passengers_num3);
			setDataById("passengers_num4",log.passengers_num4);
			setDataById("passengers_num5",log.passengers_num5);
			setDataById("passengers_num6",log.passengers_num6);
			if (log.flight_instance == "1") {
				$("#ontime").attr("checked", true);	
				var detail = log.flight_instance_log;
				var table = '';
				if(detail.indexOf("航班号 上客 上完客 关舱 开舱 下客 下完客\n") >= 0){
					var detail_detail = detail.substring(detail.indexOf("\n")+1,detail.indexOf(";"));
					var times = detail_detail.split(",");
					table += '<tr><td>航班号</td><td>上客</td><td>上完客</td><td>关舱</td><td>开舱</td><td>下客</td><td>下完客</td></tr>';
				    for (var i=0; i<times.length; i++) {
					        if (i % 7 == 0) {
						table += '<tr>';
						table += '<td><input type="text" class="form-control" value="'+ times[i] +'" /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+1] +'" style="background-color: white;"  readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+2] +'" style="background-color: white;" readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+3] +'" style="background-color: white;" readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+4] +'" style="background-color: white;"  readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+5] +'" style="background-color: white;" readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+6] +'" style="background-color: white;" readonly /></td>'
						table += "</tr>";
					    }
				    }
				}
				table += '<tr><td colspan="7"><textarea class="form-control" placeholder="其他说明" cols="">'+ detail.substring(detail.indexOf(";")+1,detail.length) +'</textarea></td></tr>';			
			    $("#fligtOntimeDetail").empty();
                $("#fligtOntimeDetail").append(table);
			} else{
				$("#late").attr("checked", true);
				
				var LateType =  log.flight_instance_n_type;
				for (var j=0; j<LateType.length; j++) {
					$("#LateType"+LateType[j]).attr("checked", true);
				}
				$("#LateTime"+log.flight_instance_n_time).attr("checked", true);
				
				var detail = log.flight_instance_log_n;
				var table = '';
				if(detail.indexOf("航班号 上客 上完客 关舱 开舱 下客 下完客\n") >= 0){
					var detail_detail = detail.substring(detail.indexOf("\n")+1,detail.indexOf(";"));
					var times = detail_detail.split(",");
					table = '<tr><td>航班号</td><td>上客</td><td>上完客</td><td>关舱</td><td>开舱</td><td>下客</td><td>下完客</td></tr>';
				    for (var i=0; i<times.length; i++) {
					    if (i % 7 == 0) {
						table += '<tr>';
						table += '<td><input type="text" class="form-control" value="'+ times[i] +'" /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+1] +'" style="background-color: white;"  readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+2] +'" style="background-color: white;" readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+3] +'" style="background-color: white;" readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+4] +'" style="background-color: white;"  readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+5] +'" style="background-color: white;" readonly /></td>'
						table += '<td><input type="text" class="form-control" name="timePicker" value="'+ times[i+6] +'" style="background-color: white;" readonly /></td>'
						table += "</tr>";
					   }
				    }
				}
				table += '<tr><td colspan="7"><textarea class="form-control" placeholder="其他说明" cols="">'+ detail.substring(detail.indexOf(";")+1,detail.length) +'</textarea></td></tr>';
			    $("#flightLateDetail").empty();
                $("#flightLateDetail").append(table);
			}
			
			setDataById("exception_old",log.exception_old);
			setDataById("exception_child",log.exception_child);
			setDataById("exception_longya",log.exception_longya);
			setDataById("exception_chair",log.exception_chair);
			setDataById("exception_vip",log.exception_vip);
			setDataById("exception_other",log.exception_other);
			
			setDataById("exceptions",log.exceptions);
			
			var safeEvent =  log.safe_event;
			for (var j=0; j<safeEvent.length; j++) {
				$("#safeEvent"+safeEvent[j]).attr("checked", true);
			}
			setDataById("safe_event_result",log.safe_event_result);
			
			setDataById("dept_pc",log.dept_pc);
			setDataById("dept_sw",log.dept_sw);
			setDataById("succestions",log.succestions);
			
			setDataById("sms_report",log.sms_report);
			
			setDataById("check_mark2",log.check_mark2);
			setDataById("check_mark3",log.check_mark3);
			setDataById("check_mark4",log.check_mark4);
			setDataById("check_mark5",log.check_mark5);
			setDataById("check_mark6",log.check_mark6);
			
			setDataById("check_content2",log.check_content2);
			setDataById("check_content3",log.check_content3);
			setDataById("check_content4",log.check_content4);
			setDataById("check_content5",log.check_content5);
			setDataById("check_content6",log.check_content6);
		
			setDataById("check_mark_security1",log.check_mark_security1);
			setDataById("check_content_security1",log.check_content_security1);
			
			if (log.security2 != null) {
				setDataById("check_mark_security2",log.check_mark_security2);
				setDataById("check_content_security2",log.check_content_security2);
			}
			
			if (log.security3 != null) {
				setDataById("check_mark_security3",log.check_mark_security3);
				setDataById("check_content_security3",log.check_content_security3);
			}
		}
	}
	
	function table_empty () {
		setDataById ("actype", "");
		setDataById ("flightno", "");
		setDataById ("flightcom", "");
		$("#flightvoyage").empty();
		setDataById ("pilot", "");
		setDataById("safe", "");
		$("#mps").empty();
		$(".sc").empty();
		$(".st").empty();
		$(".fp").empty();
		$("#fligtOntimeDetail").empty();
		$("#flightLateDetail").empty();
		$(".label_sc").empty();
		$(".star").empty();
	}
	
	function table_init () {
		//是否延误
		if ($("#ontime").attr("checked") == "checked") {
			$("#fligtOntimeDetail input").attr("disabled",false);
			$("#fligtOntimeDetail textarea").attr("readonly",false);
			
			$("input[name='flightLateType']").attr("disabled",true);
			$("input[name='flightLateTime']").attr("disabled",true);
			$("#flightLateDetail input").attr("disabled",true);
			$("#flightLateDetail textarea").attr("readonly",true);
		}
		if ($("#late").attr("checked") == "checked") {
			$("input[name='flightLateType']").attr("disabled",false);
			$("input[name='flightLateTime']").attr("disabled",false);
			$("#flightLateDetail input").attr("disabled",false);
			$("#flightLateDetail textarea").attr("readonly",false);
			
			$("#fligtOntimeDetail input").attr("disabled",true);
			$("#fligtOntimeDetail textarea").attr("readonly",true);
		}
		
		$("input[name='flightStatus']").on("click",function () {
			if ($(this).attr("id") == "ontime") {
				$("#fligtOntimeDetail input").attr("disabled",false);
				$("#fligtOntimeDetail textarea").attr("readonly",false);
				
				$("input[name='flightLateType']").attr("disabled",true);
				$("input[name='flightLateTime']").attr("disabled",true);
				$("#flightLateDetail input").attr("disabled",true);
				$("#flightLateDetail textarea").attr("readonly",true);
			} else if ($(this).attr("id") == "late") {
				$("input[name='flightLateType']").attr("disabled",false);
				$("input[name='flightLateTime']").attr("disabled",false);
				$("#flightLateDetail input").attr("disabled",false);
				$("#flightLateDetail textarea").attr("readonly",false);
				
				$("#fligtOntimeDetail input").attr("disabled",true);
				$("#fligtOntimeDetail textarea").attr("readonly",true);
			} else{
				return;
			}
		});
		
		//安全事件
		if ($("input[name='safeEventType']:checked").length != 0) {
			$("#noEvent").attr("checked",false);
			$("#safeEvent").attr("checked",true);
			$(".safeEvent").show();
			$(".Event").show();
		}
		if ($("input[name='aidEventType']:checked").length != 0 ) {
			$("#noEvent").attr("checked",false);
			$("#aidEvent").attr("checked",true);
			$(".aidEvent").show();
			$(".Event").show();
		}
		$("input[name='EventType']").on("click", function () {
			if ( $(this).attr("id") == "noEvent"){
				$("#safeEvent").attr("checked",false);
				$("#aidEvent").attr("checked",false);
				$(".Event").hide();
				$(".safeEvent").hide();
				$(".aidEvent").hide();
			} else if ( $(this).attr("id") == "safeEvent"){
				$("#noEvent").attr("checked",false);
				if ($(this).attr("checked")) {
					$(".safeEvent").show();
					$(".Event").show();
				} else{
					$(".safeEvent").hide();
				}
				if (!$("#aidEvent").attr("checked") && !$("#safeEvent").attr("checked")) {
					$(".Event").hide();
					$("#noEvent").attr("checked","checked");
				}
			} else if ( $(this).attr("id") == "aidEvent"){
				$("#noEvent").attr("checked",false);
				if ($(this).attr("checked")) {
					$(".aidEvent").show();
					$(".Event").show();
				} else{
					$(".aidEvent").hide();
				}
				if (!$("#aidEvent").attr("checked") && !$("#safeEvent").attr("checked")) {
					$(".Event").hide();
					$("#noEvent").attr("checked","checked");
				}
			} else{
				return;
			}
		});
		
	}
}